<?php

/**
 * This is the model class for table "tbl_user_profiles".
 *
 * The followings are the available columns in table 'tbl_user_profiles':
 * @property integer $id
 * @property integer $user_id
 * @property string $display_name
 * @property string $first_name
 * @property string $last_name
 * @property string $company
 * @property string $lang
 * @property string $bio
 * @property integer $dob
 * @property string $gender
 * @property string $phone
 * @property string $mobile
 * @property string $address_line1
 * @property string $address_line2
 * @property string $address_line3
 * @property string $yim_handle
 * @property string $skype_handle
 * @property string $avatar
 * @property string $website
 * @property string $facebooksite
 * @property integer $updated_on
 * @property integer $hourlyrate
 * @property integer $rate
 *
 * The followings are the available model relations:
 * @property TblUsers $user
 */
class UserProfiles extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return UserProfiles the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_user_profiles';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('gender, mobile, address_line1,  yim_handle, skype_handle', 'required'),
			array('user_id, dob, updated_on, hourlyrate, rate', 'numerical', 'integerOnly'=>true),
			array('display_name, first_name, last_name', 'length', 'max'=>50),
			array('company, yim_handle, skype_handle, avatar, facebooksite', 'length', 'max'=>100),
			array('phone, mobile', 'length', 'max'=>20),
			array('address_line1, address_line2, address_line3, website', 'length', 'max'=>255),
			array('avatar', 'file', 'types'=>'jpg,png,gif', 'allowEmpty'=>true),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'TblUsers', 'user_id'),
		);
	}
	
	// tra ve gio tinh
	public function getGender(){
		return array(
			1=>'Female',
			2=>'Male',
		);
    }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'display_name' => 'Display Name',
			'first_name' => 'First Name',
			'last_name' => 'Last Name',
			'company' => 'Company',
			'lang' => 'Lang',
			'bio' => 'Bio',
			'dob' => 'Dob',
			'gender' => 'Gender',
			'phone' => 'Phone',
			'mobile' => 'Mobile',
			'address_line1' => 'Address Line1',
			'address_line2' => 'Address Line2',
			'address_line3' => 'Address Line3',
			'yim_handle' => 'Yim Handle',
			'skype_handle' => 'Skype Handle',
			'avatar' => 'Avatar',
			'website' => 'Website',
			'facebooksite' => 'Facebooksite',
			'updated_on' => 'Updated On',
			'hourlyrate' => 'Hourlyrate',
			'rate' => 'Rate',
		);
	}
	// xu ly truoc khi luu vao csdl
	protected function beforeValidate(){
        if ($this->isNewRecord) {
            $this->user_id = Yii::app()->user->id;
            $this->updated_on = 0;
            $this->lang = 'ok';
            $this->bio = 'bodd.biz';
            $this->dob = 100;
            $this->viewed = 0;
            $this->hourlyrate = 0;
            $this->rate = 0;
        }
        return parent::beforeValidate();
    }
}